package com.hangzhou.xyj.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.DynamicPageInfoModel;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.net.handler.AddFollowHandler;
import com.hangzhou.xyj.net.handler.GetDynamicHandler;
import com.hangzhou.xyj.net.handler.GetUserDetailHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.AddFollowReq;
import com.hangzhou.xyj.net.request.CancelFollowReq;
import com.hangzhou.xyj.net.request.GetDynamicReq;
import com.hangzhou.xyj.net.request.GetUserDetailReq;
import com.hangzhou.xyj.net.response.GetUserDetailResp;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.HomePageAdapter;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;
import com.hangzhou.xyj.ui.widget.SelectMenuDialog;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.Constants;
import com.hangzhou.xyj.utils.constants.DataKey;
import com.hyphenate.easeui.EaseConstant;

/**
 * Created by Administrator on 2016/1/7.
 * 主页:包括个人，名团的信息主页
 */
public class HomePageActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView mRecyclerView;
    private UserModel mUserInfo;
    private Button mBtnFollow;
    private boolean isFollow;//是否关注
    private int mUserId;//用户ID
    private String mTitle;//标题

    @Override
    public void initTop() {
        if (StringUtil.isNotEmpty(mTitle)) {
            enableTitle(true, mTitle);
        } else {
            enableTitle(true, "标题");
        }
        enableRefresh(false);
    }

    @Override
    public void initIntentData(Intent intent) {
        mUserId = intent.getIntExtra(DataKey.INDEX, -1);
        mTitle = intent.getStringExtra(DataKey.TITLE);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_home_page);
    }

    @Override
    public void initViews() {
        mBtnFollow = (Button) findViewById(R.id.tv_home_page_follow);

        mRecyclerView = findView(R.id.rv_home_page);

        // 设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,
                LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider));
    }

    @Override
    public void initDatas() {
        getUserInfoData();
    }

    @Override
    public void initListeners() {
        findView(R.id.tv_home_page_follow).setOnClickListener(this);
        findView(R.id.btn_more_info).setOnClickListener(this);
        findView(R.id.btn_home_page_chat).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_home_page_follow:
                if (XYJApplication.getUserInfo() != null) {
                    String[] menu;
                    SelectMenuDialog menuDialog = new SelectMenuDialog(this);
                    if (isFollow) {
                        menu = new String[]{"取消关注"};
                    } else {
                        menu = new String[]{"关注"};
                    }
                    //menuDialog.setTitle("确定不再关注?");
                    menuDialog.setMenu(menu, new SelectMenuDialog.SelectMenuClickListener() {
                        @Override
                        public void onSelectMenuClick(Dialog dialog, View item, int position) {
                            dialog.dismiss();
                            if (isFollow) {
                                cancelFollow();
                            } else {
                                addFollow();
                            }
                        }
                    });
                    menuDialog.show();
                } else {
                    IntentManager.getInstance().goLoginActivity(mContext);
                }
                break;
            case R.id.btn_home_page_chat:
                if (XYJApplication.getUserInfo() != null) {
                    if (mUserInfo != null) {
                        IntentManager.getInstance().goChatActivity(mContext, mUserInfo.getUserId(), EaseConstant.CHATTYPE_SINGLE);
                    }
                } else {
                    IntentManager.getInstance().goLoginActivity(mContext);
                }
                break;
            case R.id.btn_more_info:
                Intent intent = new Intent(mContext, PersonalInfoActivity.class);
                intent.putExtra("userInfo", mUserInfo);
                IntentManager.getInstance().goPersonalInfoActivity(mContext, intent);
        }
    }


    /**
     * 添加关注
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/22 19:51
     */
    public void addFollow() {
        AddFollowReq addFollowReq = new AddFollowReq(AddFollowReq.TYPE_USER, Integer.valueOf(mUserInfo.getUserId()),
                Integer.valueOf(XYJApplication.getUserInfo().getUserId()));
        HttpLoader.getInstance().sendRequest(new AddFollowHandler(addFollowReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    ToastUtil.show("关注成功！");
                    mBtnFollow.setText("已关注");
                    isFollow = true;
                }
            }
        }, 0));
    }


    public void cancelFollow() {
        CancelFollowReq cancelFollowReq = new CancelFollowReq(CancelFollowReq.TYPE_USER, Integer.valueOf(mUserInfo.getUserId()),
                Integer.valueOf(XYJApplication.getUserInfo().getUserId()));
        HttpLoader.getInstance().sendRequest(new AddFollowHandler(cancelFollowReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    ToastUtil.show("取消关注成功！");
                    mBtnFollow.setText("关注");
                    isFollow = false;
                }
            }
        }, 0));
    }

    public void getUserInfoData() {
        GetUserDetailReq getUserDetailReq;
        //登录了
        if (mMyUserId != 0) {
            getUserDetailReq = new GetUserDetailReq(mUserId, mMyUserId);
        } else {//未登录
            getUserDetailReq = new GetUserDetailReq(mUserId);
        }
        HttpLoader.getInstance().sendRequest(new GetUserDetailHandler(getUserDetailReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                GetUserDetailResp.UserDetail getUserDetailResp = (GetUserDetailResp.UserDetail) model;
                mUserInfo = getUserDetailResp.getUserInfo();
                isFollow = getUserDetailResp.getIsFollow();
                mUserInfo.setIsFollow(isFollow);
                if (isFollow) {
                    mBtnFollow.setText("已关注");
                } else {
                    mBtnFollow.setText("关注");
                }
                //获取动态列表
                getDynamicList(1);

                //更新环信映射本地用户数据库
                XYJApplication.getInstance().updateHXUserDB(mUserInfo);
            }
        }, 0));
    }

    /**
     * 获取动态列表
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/4/6 10:08
     */
    public void getDynamicList(int currentPage) {
        //向服务器请求数据
        GetDynamicReq getDynamicReq = null;
        getDynamicReq = new GetDynamicReq(mUserId, Constants.PAGE_SIZE, currentPage);
        HttpLoader.getInstance().sendRequest(new GetDynamicHandler(getDynamicReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                DynamicPageInfoModel dynamicPageInfoModel = (DynamicPageInfoModel) model;
                mRecyclerView.setAdapter(new HomePageAdapter(mContext, mUserInfo, dynamicPageInfoModel.getDynamicList()));
            }
        }, 0));
    }
}
